import Vue from "vue";
import Router from "vue-router";
import store from "@/store";
import { getWxAuth } from "@/tool/wecharts.js";
Vue.use(Router);
// 登录注册
const login = () => import("./viewsMobile/login/index.vue");
const notFound = () => import("./viewsMobile/notFound/index.vue");
// 首页
const home = () => import("./viewsMobile/home/index.vue");
const ranking = () => import("./viewsMobile/ranking/index.vue");
const sign = () => import("./viewsMobile/signIn/index.vue");
const ydy = () => import("./viewsMobile/ydy/index.vue");
const question = () => import("./viewsMobile/question/index.vue");
const record = () => import("./viewsMobile/record/index.vue");

let router = new Router({
  mode: "history",
  base: process.env.BASE_URL,
  routes: [
    { path: "/", redirect: "/login" },
    {
      path: "/home",
      name: "home",
      component: home,
      meta: {
        name: "首页",
        showMenu: true,
        activeHeaderMenu: "home",
        isShowNav: true
      }
    },
    {
      path: "/ranking",
      name: "ranking",
      component: ranking,
      meta: {
        name: "风云榜",
        showMenu: true,
        activeHeaderMenu: "ranking",
        isShowNav: true
      }
    },
    {
      path: "/sign",
      name: "sign",
      component: sign,
      meta: {
        name: "签到",
        showMenu: true,
        activeHeaderMenu: "sign",
        isShowNav: true
      }
    },
    {
      path: "/ydy",
      name: "ydy",
      component: ydy,
      meta: {
        name: "押电影",
        showMenu: true,
        activeHeaderMenu: "ydy",
        isShowNav: true
      }
    },
    {
      path: "/question",
      name: "question",
      component: question,
      meta: {
        name: "押电影",
        showMenu: true,
        activeHeaderMenu: "question",
        isShowNav: true
      }
    },
    {
      path: "/record",
      name: "record",
      component: record,
      meta: {
        name: "福鼠记录",
        showMenu: true,
        activeHeaderMenu: "record",
        isShowNav: true
      }
    },
    {
      path: "/login",
      name: "login",
      component: login,
      meta: { name: "登录" }
    },
    {
      path: "*",
      name: "notFound",
      component: notFound,
      meta: { name: "404" }
    }
  ]
});
export default router;
function isPC() {
  let flag = true;
  let userAgentInfo = navigator.userAgent;
  let Agents = [
    "Android",
    "iPhone",
    "SymbianOS",
    "Windows Phone",
    "iPad",
    "iPod"
  ];
  for (var v = 0; v < Agents.length; v++) {
    if (userAgentInfo.indexOf(Agents[v]) > 0) {
      flag = false;
      break;
    }
  }
  return flag;
}
router.beforeEach(function(to, from, next) {
  let state = window.sessionStorage.getItem("state");
  if (state) {
    store.replaceState(JSON.parse(state));
  }
  if (to.name !== "notFound") {
    // var ua = navigator.userAgent.toLowerCase();
    // var isWeixin = ua.indexOf("micromessenger") != -1;
    // if (!isWeixin) {
    //   router.push({ name: "notFound" });
    // }
    if (isPC()) {
      router.push({ name: "notFound" });
    }
  }
  next();
});
router.afterEach(function(to) {
  if (window.__wxjs_is_wkwebview) {
    // IOS
    if (window.entryUrl == "" || window.entryUrl == undefined) {
      var url = `http://gift.hrycf.com${to.fullPath}`;
      window.entryUrl = url; // 将后面的参数去除
    }
    getWxAuth(to.fullPath, "ios");
  } else {
    // 安卓
    setTimeout(function() {
      getWxAuth(to.fullPath, "android");
    }, 500);
  }
});
